/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.user;

import com.nc.kyiv.yellow.sociallibrary.POJO.LibUser;
import com.nc.kyiv.yellow.sociallibrary.dao.UserDAO;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import com.nc.kyiv.yellow.sociallibrary.emails.EmailSender;
import java.io.*;
import java.util.Map;

/**
 *
 * @author Evgeniy Grigorevskiy
 */
public class RegisterUser implements CommonClassesInterface {

    private String[] mapKeys = {"userLogin", "userPass", "userFirstName", "userLastName", "eMail"};
    private MapBuilder helper = new MapBuilder();
    private String result;

    public RegisterUser() {
    }

    public RegisterUser(String dummy) {
    }

    @Override
    public String execute(String parameters) throws Exception {

        Map postParameters = helper.stringToMap(parameters, mapKeys,
                helper.getRequestSplitter());
        if (postParameters != null) {
            LibUser NewUser = new LibUser(postParameters, mapKeys);
            UserDAO dao = new UserDAO();
            try {
                dao.save(NewUser);
                EmailSender ES = new EmailSender();
                //Here we need to get ReceiverConfPage
                ES.Confirmation(postParameters.get("eMail").toString(),
                        postParameters.get("userFirstName").toString(),
                        postParameters.get("userLogin").toString(),
                        "http://vk.com");
            } catch (Exception ex) {
                return "failed";
            }

            return "success";
        } else {
            return "failed";
        }

    }
}
